/**  
 * Project Name:mmc_svn  
 * File Name:FtpConManager.java  
 * Package Name:com.mes.basicdata.service.hbydMPM.impl  
 * Date:May 6, 20176:53:21 PM  
 * Copyright (c) 2017, MM-MES All Rights Reserved.  
*/  
  
package com.mes.integration.hbydMPM.impl;  
/**  
 * ClassName:FtpConManager <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     May 6, 2017 6:53:21 PM <br/>  
 * @author   Administrator  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
  
import org.apache.commons.io.IOUtils;  
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPReply;  
  
/** 
 *<p>ftp连接管理(使用apache commons-net-1.4.1 lib)</p> 
 * 
 * 
 */  
public class FtpConManager {  
    private static FtpConManager instance;  
    private FTPClient ftpClient =null;  
      
    /** 
     * get FtpConManager 
     * @return FtpConManager 
     */  
    public synchronized static FtpConManager getInstance(){  
        if( instance == null ){  
            instance = new FtpConManager();  
        }  
        return instance;  
    }  
  
      
    /** 
     * <p>ftp��¼</p> 
     * @param s_url ftp�����ַ 
     * @param uname �û��� 
     * @param pass  ���� 
     */  
    public void login(String s_url,String uname,String pass){  
        ftpClient = new FTPClient();  
        try{  
            //����  
            ftpClient.connect(s_url);  
            ftpClient.login(uname,pass);  
            //��������Ƿ�ɹ�  
            int reply = ftpClient.getReplyCode();  
            if(!FTPReply.isPositiveCompletion(reply)) {  
                this.closeCon();  
                System.err.println("FTP server refused connection.");  
                System.exit(1);  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
            //�ر�  
            this.closeCon();  
        }  
    }  
      
      
    /** 
     * <p>ftp�ϴ��ļ�</p> 
     * @param srcUrl ���ϴ��ļ� 
     * @param targetFname ����Ŀ���ļ� 
     * @return true||false 
     */  
    public boolean uploadFile(String srcUrl,String targetFname){  
        boolean flag = false;  
        if( ftpClient!=null ){  
             File srcFile = new File(srcUrl);   
             FileInputStream fis = null;  
             try {  
                fis  = new FileInputStream(srcFile);  
                  
                //�����ϴ�Ŀ¼   
                ftpClient.changeWorkingDirectory("/ImData/");  
                ftpClient.setBufferSize(1024);   
                ftpClient.setControlEncoding("GBK");   
                  
                //�����ļ����ͣ������ƣ�   
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
                //�ϴ�  
                flag = ftpClient.storeFile(targetFname, fis);   
            } catch (Exception e) {  
                e.printStackTrace();  
                this.closeCon();  
            }finally{  
                IOUtils.closeQuietly(fis);  
            }  
        }  
        return flag;  
    }//end method uploadFile  
      
      
    /** 
     *  
     * <p>ɾ��ftp�ϵ��ļ�</p> 
     * @param srcFname 
     * @return true || false 
     */  
    public boolean removeFile(String srcFname){  
        boolean flag = false;  
        if( ftpClient!=null ){  
            try {  
                flag = ftpClient.deleteFile(srcFname);  
            } catch (IOException e) {  
                e.printStackTrace();  
                this.closeCon();  
            }  
        }  
        return flag;  
    }  
      
    /** 
     *<p>����ftp����</p>  
     */  
    public void closeCon(){  
        if(ftpClient !=null){  
            if(ftpClient.isConnected()){  
                try {  
                    ftpClient.logout();  
                    ftpClient.disconnect();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }   
            }  
        }  
    }  
}  
  
